package com.qiyou.service;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.qiyou.dao.IPlaceAttestationDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.PlaceAttestation;
import com.qiyou.util.LogUtil;

/**
 * 请求认证景点
 */
@WebServlet("/ScenicSpotAuthenticationService")
public class ScenicSpotAuthenticationService extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final Logger log = LogUtil
            .getLogger(ScenicSpotAuthenticationService.class);

    public ScenicSpotAuthenticationService() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");

        String userID = request.getParameter("userID");
        String sceIDStr = request.getParameter("sceID");
        long sceID = 0L;
        try {
            sceID = Long.parseLong(sceIDStr);
        } catch (NumberFormatException e) {
            sceID = 0L;
        }

        PlaceAttestation placeAttestation = new PlaceAttestation();
        placeAttestation.setUserID(userID);
        placeAttestation.setSceID(sceID);

        IPlaceAttestationDao dao = DaoFactory.getPlaceAttestationDao();
        boolean result = false;
        try {
            result = dao.doCreate(placeAttestation);
        } catch (SQLException e) {
            log.error(e);
        }
        log.info("用户:" + userID + ", 景点:" + sceIDStr + ", 认证结果:" + result);
    }

}
